//着色器代码

/*
const vertice_ground_source = `#version 300 es
	precision mediump float;
	in vec3 aPos;
	
	uniform mat4 pers;
	uniform mat4 rota;
	//uniform float time;
	uniform vec3 move;
	
	out vec3 normal;//法线
	out vec3 position;//位置，世界坐标
	
	
	void main()
	{

		
		normal = vec3(aPos.x, aPos.z, aPos.y);

		position = 0.2 * vec3(aPos.x, aPos.z, aPos.y);
		
		gl_Position = pers*rota*vec4(position.x + move.x, position.y + move.y, position.z + move.z, 1.0);
	}`;
	
const fragment_ground_source = `#version 300 es
	precision mediump float;
	in vec3 normal;
	in vec3 position;
	
	uniform vec3 move;

	out vec4 FragColor;
	
	//法线分布函数
	float D_GGX_TR(vec3 N, vec3 H, float a)
{
	const float PI = 3.1415926;
	
    float a2     = a*a;
    float NdotH  = max(dot(N, H), 0.0);
    float NdotH2 = NdotH*NdotH;

    float nom    = a2;
    float denom  = (NdotH2 * (a2 - 1.0) + 1.0);
    denom        = PI * denom * denom;

    return nom / denom;
}
	
	//菲涅尔
vec3 fresnelSchlick(float cosTheta, vec3 F0)
{
    return F0 + (vec3(1.0) - F0) * pow(1.0 - cosTheta, 5.0);
}
	
	void main() {
		vec3 viewPos = vec3(-move.x, -move.y, -move.z);
		
		vec3 lightDir = vec3(0.0f, 0.7071f, -0.7071f);//光源方向
		//vec3 reflectDir = reflect(-lightDir, normal);
		vec3 viewDir = normalize(viewPos - position);
		vec3 half_vec = normalize(lightDir + viewDir);//计算半程向量
		float mir =0.1 * D_GGX_TR(normal, half_vec, 0.1);

		
		//float spec = pow(max(dot(viewDir, reflectDir), 0.0), 64.0) * 0.5 + 0.5;
		float env = max(dot(lightDir, normal), 0.0);
		//float color_result = env*1.0 + mir*1.0;
		
		
		//高度渐变 模拟散射
		float height_wave = position.y + 0.35;
		
		vec3 color_tmp = vec3(0.4 * env + mir + 0.25
		 );
		
		//菲涅尔现象
		//vec3 fresnel = fresnelSchlick( dot(viewDir, normal), vec3(0.03));
		
		FragColor = vec4(color_tmp,
					1.0);
		//FragColor = vec4(normal, 1.0);
	}
	`;
	
*/
	
const vertex_cube_source = `#version 300 es
	precision mediump float;

	in vec3 aPos;
	in vec3 norm;
	in vec2 coord;
	
	uniform mat4 pers;
	uniform mat4 rota;
	uniform mat4 partrota;
	uniform vec3 move;
	
	out vec3 normal;//法线
	out vec2 coord_frag;
	//out vec3 position;//位置，世界坐标
	
	
	void main()
	{
		vec4 normtmp = partrota * vec4(norm, 1.0);
		normal = normtmp.xyz;
		coord_frag = coord;

		//position = 1.0 * aPos;
		vec4 positiontmp = partrota * vec4(aPos, 1.0) + vec4(move, 0.0);
		gl_Position = pers*rota*positiontmp;
	}
	
	`;
	
const fragment_cube_source = `#version 300 es
	precision mediump float;

	in vec3 normal;
	in vec2 coord_frag;
	//in vec3 position;
	

	out vec4 FragColor;
	
	uniform sampler2D uSampler;//纹理采样
	
	void main()
	{
		vec3 lightDir = vec3(0.0, 0.7071, -0.7071);//光源方向
		float env = max(dot(lightDir, normal), 0.0);
		
		FragColor = vec4(vec3(0.5 * env + 0.4),1.0) * texture(uSampler, coord_frag);
	}
	
	`;
	
	
//ground着色器代码	
const vertex_ground_source = `#version 300 es
	precision mediump float;
	in vec3 norm;
	in vec3 aPos;
	
	
	uniform mat4 pers;
	uniform mat4 rota;
	uniform vec3 move;
	
	out vec3 normal;//法线
	//out vec3 position;//位置，世界坐标
	
	
	void main()
	{
		normal = norm;

		vec4 positiontmp = vec4(aPos, 1.0) + vec4(move, 0.0);
		gl_Position = pers*rota*positiontmp;
	}
	
	`;
	
const fragment_ground_source = `#version 300 es
	precision mediump float;
	in vec3 normal;
	//in vec3 position;
	

	out vec4 FragColor;
	
	
	void main()
	{
		vec3 lightDir = vec3(0.0f, 0.7071f, -0.7071f);//光源方向
		float env = max(dot(lightDir, normal), 0.0);
		
		FragColor = vec4(vec3(0.5 * env + 0.4),1.0);
	}
	
	`;